package tests;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;

import tester.utilities.*;

import tester.*;

@Example
public class FindMethodsExample {
	
	private Method[] findAllMethods(Class<?> c){
		Class<?> classToSurvey = c;
		
		ArrayList<Method> list = new ArrayList<Method>();
		while (classToSurvey != null  && classToSurvey != Object.class) {
			Method[] allMethods = classToSurvey.getDeclaredMethods();
			for (Method m : allMethods) {
				m.setAccessible(true);
				list.add(m);
			}
			
			classToSurvey = classToSurvey.getSuperclass();
		}

		return list.toArray(new Method[0]);
	}
	
	@TestMethod
	public void testFindAll(Tester t){
		ArrayList<Method> list = new ArrayList<Method>();
	
		for(Method m : Bar.class.getDeclaredMethods()){
			m.setAccessible(true);
			list.add(m);
		}
		
		for(Method m : Foo.class.getDeclaredMethods()){
			m.setAccessible(true);
			list.add(m);
		}
		
		Method[] methods = list.toArray(new Method[0]);
		Method[] allFound = findAllMethods(Bar.class);
		
		boolean result = true;
		int bad = -1;
		for(int i = 0; i < methods.length && result; i++){
			//result = new Inspector().isSame(methods[i], allFound[i]);
			//bad = i;
		}
		
		for(Method m : methods){
			try {
					System.out.println(m.toString());
					Object o = m.invoke(new Bar(), new Object[0]);
					if(o != null){
						System.out.println(o.toString());	
					}
					
					System.out.println("------------------------");
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		for(Method m : allFound){
			System.out.println(m.toString());
		}
		
		//t.checkExpect(result);
		//t.checkExpect(methods, allFound);		
		
		
	}
	
	public static void main(String[] args){
		Tester.runFullReport(new FindMethodsExample());
	}

}

class Foo{
	
	public Foo(){}
	
	public void randomMethod(){}
	
	public int anotherThing() { return 0; }
}

class Bar extends Foo{
	
	public Bar(){}
	
	public void fooBar(){}
	
	public int anotherThing() { return 1; }
}
